#pragma once

#include "Comm.hpp"
#include <iostream>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class InetAddr
{
private:
    void PortNet2Host()
    {
        _port = ::ntohl(_net_addr.sin_port);
    }
    void IpNet2Host()
    {
        //将网络风格转化为字符串风格的
        char ipbuffer[64];
        const char * ip = inet_ntop(AF_INET,&_net_addr.sin_addr,ipbuffer,sizeof(ipbuffer));
        (void)ip;
        _ip = ipbuffer;
    }
public:
    InetAddr()
    {

    }
    //网络转点分十进制---这里是网络转主机
    InetAddr(const struct sockaddr_in & addr):_net_addr(addr)
    {
        PortNet2Host();
        IpNet2Host();
    }
    InetAddr(uint16_t port):_port(port),_ip("")
    {
        _net_addr.sin_family = AF_INET;
        _net_addr.sin_port = htons(_port);
        //这里是主机转网络
        _net_addr.sin_addr.s_addr = INADDR_ANY;
    }
    struct sockaddr* NetAddr()
    {
        return CONV(&_net_addr);
    }
    socklen_t NetAddrLen()
    {
        return sizeof(_net_addr);
    }
    std::string Ip()
    {
        return _ip;
    }
    std::uint16_t Port()
    {
        return _port;
    }
    std::string Addr()
    {
        return Ip() + ":" + std::to_string(Port());
    }
    bool  operator==(const InetAddr &u)
    {
        return _ip == u._ip;
    }
    ~InetAddr()
    {

    }
private:
    //网络addr
    struct sockaddr_in _net_addr;
    //网络序列的端口
    uint16_t _port;
    std::string _ip;
};